<?php

namespace App\Models\setting;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use App\Models\setting\CmcSystemConfig as SystemConfig;
use Illuminate\Database\Eloquent\Model;

class CmcSystemConfigTab extends Model
{
	use HasDateTimeFormatter;
    

    protected $table = 'cmc_system_config_tab';

    
    public $timestamps = false;


    /**
     * 获取单选按钮或者多选按钮的显示值
     * @param $menu_name
     * @param $value
     * @return string
     */
    public static function getRadioOrCheckboxValueInfo($menu_name, $value)
    {
        $parameter = [];
        $option = [];
        $config_one = SystemConfig::getOneConfig('menu_name', $menu_name);
        $parameter = explode("\n", $config_one['parameter']);
        foreach ($parameter as $k => $v) {
            if (isset($v) && strlen($v) > 0) {
                $data = explode('=>', $v);
                $option[$data[0]] = $data[1];
            }
        }
        $str = '';
        if (is_array($value)) {
            foreach ($value as $v) {
                $str .= $option[$v] . ',';
            }
        } else {
            $str .= !empty($value) ? $option[$value] : $option[0];
        }
        return $str;
    }

    /**
     * 获取全部
     * @param int $type
     * @return mixed
     */
    public static function getAll($type = 0)
    {

        $where['status'] = 1;
        $where['pid'] = 0;
        if ($type > -1) $where['type'] = $type;
        return self::where($where)
            ->orderBy('sort','desc')
            ->orderBy('id' ,'asc')
            ->get();
    }

    /**
     * @param $pid
     * @return mixed
     */
    public static function getChildrenTab($pid)
    {
        $where['status'] = 1;
        $where['pid'] = $pid;
        return self::where($where)
            ->orderBy('sort','desc')
            ->orderBy('id' ,'asc')
            ->get();
    }

    /**
     * 获取配置分类
     * @param array $where
     * @return array
     */
    public static function getSystemConfigTabPage($where = [])
    {
        $model = new self;
        if ($where['title'] != '') $model = $model->where('title', 'LIKE', "%$where[title]%");
        if ($where['status'] != '') $model = $model->where('status', $where['status']);
        $model = $model->where('pid', $where['pid']);

        return $model->paginate(10);

    }



}
